package com.nintendo.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.nintendo.dto.EbookDTO;
import com.nintendo.entity.Category;
import com.nintendo.entity.Ebook;
import com.nintendo.mapper.CategoryMapper;
import com.nintendo.mapper.EbookMapper;
import com.nintendo.service.EbookService;
import com.nintendo.util.SnowFlake;
import com.nintendo.vo.EbookCascaderVo;
import com.nintendo.vo.EbookCascaderVo2;
import com.nintendo.vo.EbookVO;
import com.nintendo.vo.PageInfoVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
@Service
public class EbookServiceImpl implements EbookService {
    @Autowired
    private EbookMapper ebookMapper;
    @Autowired
    private SnowFlake snowFlake;
    @Autowired
    private CategoryMapper categoryMapper;
    @Override
    public List<Ebook> findAllEbooks() {
        return ebookMapper.selectAllEbooks();
    }

    @Override
    public PageInfoVo findAllEbooksByName(EbookDTO ebookDTO) {
        PageHelper.startPage(ebookDTO.getPageNum(),5);
        List<EbookVO> ebooks = ebookMapper.selectEbooksByName(ebookDTO);

        List<EbookVO> ebookVOS = new ArrayList<>();
        ebooks.forEach((ebook)->{
            EbookVO ebookVO = new EbookVO();
            BeanUtils.copyProperties(ebook,ebookVO);
            ebookVOS.add(ebookVO);
        });
        PageInfo<EbookVO> pageInfo = new PageInfo<>(ebookVOS);
        long total = pageInfo.getTotal();
        PageInfoVo pageInfoVo =new PageInfoVo<>();
        pageInfoVo.setTotal(total);
        pageInfoVo.setData(ebookVOS);

        return pageInfoVo;
    }

    @Override
    public void deleteById(Long id) {
        ebookMapper.deleteById(id);
    }

    @Override
    public EbookVO findEbookById(Long id) {
        return ebookMapper.selectById(id);
    }

    @Override
    public void updateEbookById(Ebook ebook) {
        ebookMapper.updateEbookById(ebook);
    }

    @Override
    public void saveEbook(Ebook ebook) {
        Long l = snowFlake.nextId();
        String s = l.toString();
        String substring = s.substring(0, 8);
        ebook.setId(Long.valueOf(substring));
        ebookMapper.saveEbook(ebook);
    }

    @Override
    public List<EbookCascaderVo> getEbookCascaderVo() {
        List<EbookCascaderVo> ebookCascaderVos = new ArrayList<>();
        List<Category> categories = categoryMapper.findAll();
        categories.forEach(category -> {
            if(category.getParent()==0){
                List<Category> allByParent = categoryMapper.findAllByParent(category.getId());
                List<EbookCascaderVo2> ebookCascaderVo2s = new ArrayList<>();
                allByParent.forEach(category2 -> {
                    ebookCascaderVo2s.add(new EbookCascaderVo2(category2.getId(),category2.getName()));
                });
                ebookCascaderVos.add(new EbookCascaderVo(category.getId(),category.getName(),ebookCascaderVo2s));
            }
        });
        return ebookCascaderVos;
    }

    @Override
    public List<Ebook> findByCategoryId2(Long id) {
        return ebookMapper.findByCategoryId2(id);
    }
}
